package com.example.basic.web;

import com.example.basic.core.result.Result;
import com.example.basic.core.result.ResultGenerator;
import com.example.basic.custom.Log;
import com.example.basic.exception.ServiceException;
import com.example.basic.web.service.login.LoginService;
import com.example.basic.web.vo.login.LoginParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "登录相关操作", tags = {"登录相关操作"})
@RestController
@RequestMapping("/api")
public class LoginController {

    @Resource
    private LoginService loginService;

    @Log(title = "登录相关操作", description = "pc端登录")
    @ApiOperation(value="登录",notes = "pc端登录")
    @PostMapping("/login")
    public Result<LoginParams> login(
            @ApiParam(name = "loginName", value = "登录名", required = true) @RequestParam(name = "loginName", required = true) String loginName,
            @ApiParam(name = "loginPwd", value = "密码", required = true) @RequestParam(name = "loginPwd", required = true) String loginPwd,
            @ApiParam(name = "sysType", value = "系统类型，1-pc,2-移动端", required = true) @RequestParam(name = "sysType", required = true) String sysType) throws Exception {
        return ResultGenerator.genSuccessResult(loginService.login(loginName,loginPwd,sysType));
    }

    @Log(title = "登录相关操作", description = "pc端退出登录")
    @ApiOperation(value="退出",notes = "pc端退出登录")
    @PostMapping("/loginOut")
    public Result loginOut(
            @ApiParam(name = "loginName", value = "登录名") @RequestParam String loginName) throws Exception {

        boolean result = loginService.loginOut(loginName);
        if(result){
            return ResultGenerator.genSuccessResult("登出成功！");
        }
        throw new ServiceException("登出失败！");
    }
}
